package com.ruoyi.ylyx.controller;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.ylyx.domain.YlyxRecruitpost;
import com.ruoyi.ylyx.service.IYlyxRecruitpostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ylyx.domain.YlyxRecuittype;
import com.ruoyi.ylyx.service.IYlyxRecuittypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 招聘类别Controller
 * 
 * @author yuexl
 * @date 2021-02-05
 */
@Controller
@RequestMapping("/ylyx/recuittype")
public class YlyxRecuittypeController extends BaseController
{
    private String prefix = "ylyx/recuittype";

    @Autowired
    private IYlyxRecuittypeService ylyxRecuittypeService;
    @Autowired
    private IYlyxRecruitpostService ylyxRecruitpostService;

    @RequiresPermissions("ylyx:recuittype:view")
    @GetMapping()
    public String recuittype()
    {
        return prefix + "/recuittype";
    }

    /**
     * 查询招聘类别列表
     */
    @RequiresPermissions("ylyx:recuittype:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YlyxRecuittype ylyxRecuittype)
    {
        startPage();
        List<YlyxRecuittype> list = ylyxRecuittypeService.selectYlyxRecuittypeList(ylyxRecuittype);
        return getDataTable(list);
    }

    /**
     * 导出招聘类别列表
     */
    @RequiresPermissions("ylyx:recuittype:export")
    @Log(title = "招聘类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YlyxRecuittype ylyxRecuittype)
    {
        List<YlyxRecuittype> list = ylyxRecuittypeService.selectYlyxRecuittypeList(ylyxRecuittype);
        ExcelUtil<YlyxRecuittype> util = new ExcelUtil<YlyxRecuittype>(YlyxRecuittype.class);
        return util.exportExcel(list, "recuittype");
    }

    /**
     * 新增招聘类别
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存招聘类别
     */
    @RequiresPermissions("ylyx:recuittype:add")
    @Log(title = "招聘类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YlyxRecuittype ylyxRecuittype)
    {
        return toAjax(ylyxRecuittypeService.insertYlyxRecuittype(ylyxRecuittype));
    }

    /**
     * 修改招聘类别
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        YlyxRecuittype ylyxRecuittype = ylyxRecuittypeService.selectYlyxRecuittypeById(id);
        mmap.put("ylyxRecuittype", ylyxRecuittype);
        return prefix + "/edit";
    }

    /**
     * 修改保存招聘类别
     */
    @RequiresPermissions("ylyx:recuittype:edit")
    @Log(title = "招聘类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YlyxRecuittype ylyxRecuittype)
    {
        return toAjax(ylyxRecuittypeService.updateYlyxRecuittype(ylyxRecuittype));
    }

    /**
     * 删除招聘类别
     */
    @RequiresPermissions("ylyx:recuittype:remove")
    @Log(title = "招聘类别", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] arrayIds = Convert.toStrArray(ids);
        for(int i=0;i<arrayIds.length;i++){
            YlyxRecruitpost ylyxRecruitpost = new YlyxRecruitpost();
            ylyxRecruitpost.setRecruitType(arrayIds[i]);
            if(ylyxRecruitpostService.selectYlyxRecruitpostList(ylyxRecruitpost).size()>0){
                return error("该分类存在有相关的招聘岗位，不能删除！");
            }
        }
        return toAjax(ylyxRecuittypeService.deleteYlyxRecuittypeByIds(ids));
    }
}
